package com.zhufei.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zhufei.dao.NewsDao;
import com.zhufei.dao.Paging;
import com.zhufei.dao.ZFAdministratorDao;
import com.zhufei.dao.ZhuFeiUserInfoDao;
import com.zhufei.entity.ZFAdministrator;

public class adminServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public adminServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String action = request.getParameter("action");
		if(action == null){
			
		}
		if(action.equals("add")){
			this.add(request,response);
		}
		if(action.equals("manage")){
			this.manage(request,response);
		}
		if(action.equals("delete")){
			this.detele(request,response);
		}
		if(action.equals("changePassword")){
			this.modifyPsw(request,response);
		}
		if(action.equals("login")){
			this.login(request,response);
		}
	}

	private void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=utf8");
		String adminName = request.getParameter("adminName");
		String password = request.getParameter("adminPassword");
		ZFAdministratorDao adminDao = new ZFAdministratorDao();
		ZFAdministrator admin = adminDao.getAdministrator(adminName);
		if(admin.getAdministrator_name()== null||!admin.getPassword().equals(password)){
			PrintWriter   out   =   response.getWriter(); 
			out.write( " <script   language=javascript> alert( '用户名不存在或密码错误！ ');window.location.href= 'adminLogin.jsp'; </script> "); 
			out.flush(); 
			out.close(); 
		}else{
			HttpSession session = request.getSession();
			session.setAttribute("admin", admin);
			session.setAttribute("isLogin", "1");
			response.sendRedirect("admin/maind.jsp");
			
		}
		
	}

	private void modifyPsw(HttpServletRequest request,
			HttpServletResponse response) throws IOException { 
		response.setContentType("text/html;charset=utf8");
		String adminName = request.getParameter("adminName");
		String oldPsw = request.getParameter("oldPsw");
		String newPsw = request.getParameter("newPassword");
		ZFAdministratorDao adminDao = new ZFAdministratorDao();	
		ZFAdministrator admin = adminDao.getAdministrator(adminName);
		if(!admin.getPassword().equals(oldPsw)){
			PrintWriter   out   =   response.getWriter(); 
			out.write( " <script   language=javascript> alert( '原密码错误！ ');window.location.href= 'admin/modifyPassword.jsp'; </script> "); 
			out.flush(); 
			out.close(); 			
		}else{
		boolean isSucc = adminDao.updateAdministrator(admin.getID(), newPsw, admin.getRegion(), admin.getClass_());
		if(isSucc){
			PrintWriter   out   =   response.getWriter(); 
			out.write( " <script   language=javascript> alert( '修改密码成功！ ');window.location.href= 'admin/adminIndex.jsp'; </script> "); 
			out.flush(); 
			out.close(); 
		}else{
			PrintWriter   out   =   response.getWriter(); 
			out.write( " <script   language=javascript> alert( '修改密码失败！ ');window.location.href= 'admin/modifyPassword.jsp'; </script> "); 
			out.flush(); 
			out.close(); 			
		}
		}
	}

	private void detele(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=utf8");
		String id = request.getParameter("id");
		ZFAdministratorDao admin = new ZFAdministratorDao();
		boolean isSucc = admin.deleteAdministrator(id);
		if(isSucc){
			PrintWriter   out   =   response.getWriter(); 
			out.write( " <script   language=javascript> alert( '删除管理员成功！ ');window.location.href= 'adminServlet?action=manage'; </script> "); 
			out.flush(); 
			out.close(); 	
		}else{
			PrintWriter   out   =   response.getWriter(); 
			out.write( " <script   language=javascript> alert( '删除管理员失败！ ');window.location.href= 'adminServlet?action=manage'; </script> "); 
			out.flush(); 
			out.close(); 			
		}
	}

	private void manage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=utf8");
		ZFAdministratorDao admin = new ZFAdministratorDao();
		int adminCount = admin.getCountOfAdministrator();
		ArrayList adminList = admin.getAdministrators(0, adminCount);
		HttpSession session = request.getSession();
		session.setAttribute("adminList", adminList);
		Paging paging = new Paging();
		//取得page值
		String strPage = paging.strPage(request, "page");
		int current_Page = paging.current_Pages(strPage);
		//设置一页显示多少行数据
		int page_record = paging.setPage_record(10);
		//取得总页数
		int total_page = paging.getTotal_Pages(adminCount);
		//保存当前页数
		session.setAttribute("current_page", current_Page);
		//保存总页数
		session.setAttribute("total_page", total_page);
		session.setAttribute("page_record", page_record);
		response.sendRedirect("admin/manageAdmin.jsp");
	}



	private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=utf8");
		String adminName = request.getParameter("adminN");
		String adminPsw = request.getParameter("adminP");
		String region = request.getParameter("region");
		int authority = Integer.parseInt(request.getParameter("authority"));
		ZFAdministratorDao admin = new ZFAdministratorDao();
		boolean isSucc = admin.insertAdministrator(adminName, adminPsw, region, authority);
		if(isSucc){
			PrintWriter   out   =   response.getWriter(); 
			out.write( " <script   language=javascript> alert( '添加管理员成功！ ');window.location.href= 'adminServlet?action=manage'; </script> "); 
			out.flush(); 
			out.close(); 
		}else{
			PrintWriter   out   =   response.getWriter(); 
			out.write( " <script   language=javascript> alert( '添加管理员失败！ ');window.location.href= 'adminServlet?action=manage'; </script> "); 
			out.flush(); 
			out.close(); 			
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
